<?php
namespace app\system\controller;

use app\common\model\Region;
use app\system\model\GroupModel;
use app\system\model\ManageModel;
use app\system\validate\Manager;

class Manage extends Common
{
    protected $pageSize = 20;

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $keywords = input('get.keywords', '', 'trim');
        $locks = input('get.locks', 0, 'intval');

        $where = [];

        $keywords && $where['m.username|m.truename|m.tel'] = ['like', "%$keywords%"];
        $locks && $where['m.locks'] = $locks;

        $manageModel = new ManageModel();
        $list = $manageModel->selectManage($where, $this->pageSize);

        $assign = [
            'list' => $list,
        ];
        return $this->fetch('', $assign);
    }

    /**
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add()
    {
        if (isAjaxPost()) {
            $data = input('post.');

            //表单自动验证
            $validate = new Manager();
            if (!$validate->check($data)) {
                return $this->jsonError($validate->getError());
            }

            $data['time'] = time();
            $data['password'] = passmd($data['password']);
            $manageModel = new ManageModel();
            if ($manageModel->addManage($data)) {
                return $this->jsonSuccess('添加成功');
            } else {
                return $this->jsonError('添加失败!');
            }
        }

        $groupModel = new GroupModel();
        $groupList = $groupModel->selectGroup();

        $areaModel = new Region();
        $shengList = $areaModel->selectRegion(['pid'=>0]);

        $assign = [
            'groupList' => $groupList,
            'shengList' => $shengList,
        ];
        return $this->fetch('', $assign);
    }

    /**
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function save()
    {
        $manageModel = new ManageModel();

        if (isAjaxPost()) {
            $data = input('post.');
            if (empty($data['password'])) {
                unset($data['password']) ;
            } else {
                $data['password'] = passmd($data['password']);
            }
            //表单自动验证
            $validate = new Manager();
            if (!$validate->scene('edit')->check($data)) {
                return $this->jsonError($validate->getError());
            }

            if ($manageModel->updateManage($data, ['id'=>$data['id']]) !== false) {
                return $this->jsonSuccess('修改成功');
            } else {
                return $this->jsonError('修改失败!');
            }
        }

        $manageId = input('id', 0, 'intval');

        $manageInfo = $manageModel->findManage(['id'=>$manageId]);

        if (empty($manageInfo)) {
            $this->redirect('index');
        }

        $groupModel = new GroupModel();
        $groupList = $groupModel->selectGroup();

        $areaModel = new Region();
        $shengList = $areaModel->selectRegion(['pid'=>0]);
        $cityList = [];
        $areaList = [];

        if ($manageInfo['sheng_id'] > 0) {
            $cityList = $areaModel->selectRegion(['pid'=>$manageInfo['sheng_id']]);
        }

        if ($manageInfo['city_id'] > 0) {
            $areaList = $areaModel->selectRegion(['pid'=>$manageInfo['city_id']]);
        }

        $assign = [
            'data' => $manageInfo,
            'groupList' => $groupList,
            'shengList' => $shengList,
            'cityList' => $cityList,
            'areaList' => $areaList,
        ];
        return $this->fetch('', $assign);
    }

    public function del($id)
    {
        if (isAjaxPost()) {
            $id = intval($id);
            if ($id <= 0) {
                return $this->jsonError('删除失败');
            }

            $manageModel = new ManageModel();
            if ($manageModel->delManage($id)) {
                return $this->jsonSuccess('删除成功');
            } else {
                return $this->jsonError('删除失败');
            }
        }
    }

    public function locks($id, $locks)
    {
        if (isAjaxPost()) {
            $id = intval($id);
            $locks = intval($locks);
            if ($id <= 0 || !in_array($locks, [0, 1])) {
                return $this->jsonError('操作失败');
            }

            $manageModel = new ManageModel();
            if ($manageModel->updateManage(['locks' => $locks], ['id' => $id]) !== false) {
                return $this->jsonSuccess('操作成功');
            } else {
                return $this->jsonError('操作失败');
            }
        }
    }

}